package hot100;

public class productExceptSelf2 {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] ret = new int[n];
        int[] pre =  new int[n+1];
        int[] aft =  new int[n+1];
        pre[0] = 1;
        aft[n - 1] = 1;
        //i的前缀积*后缀积
        for(int i = 1,j = n - 2;i < n;i++,j--) {
            pre[i] = nums[i-1] * pre[i - 1];
            aft[j] = nums[j+1] * aft[j + 1];
        }
        for(int x:pre) {
            System.out.print(x + " ");
        }
        System.out.println();
        for(int x:aft) {
            System.out.print(x + " ");
        }
        //开始计算
        for(int i = 0;i < n;i++) {
            ret[i] = pre[i] * aft[i];
        }
        return ret;
    }
}
